﻿
// 创建人  ：LinJian
// 创建时间：2006-09-04
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.Security;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ChBusiness.Menu
{
    /// <!--
    /// 功能描述  : 菜单操作类
    /// 创建人  : LinJian
    /// 创建时间: 2006-09-04
    /// -->
    public class ClsMenuOperation : Page
    {
        private Database dbTemp = BasicOperate.CreateNetOfficeDataBase();

        /// <summary>
        /// 根据系统标识生成菜单
        /// </summary>
        /// <returns></returns>
        public  DataTable GenerateParentMenu(string userID)
        {
            string strSql = @"select a.* from sys_menu a join sys_usertype_menu_relation b on a.ID=b.Menu_ID join sys_usertype c on c.id=b.usertype_id join sys_user d on d.Type=c.UserTypeCode  where a.parent_id is null and d.userID=:userID order by ORDER_FIELD asc";
            DbCommand cmd = dbTemp.GetSqlStringCommand(strSql);
            dbTemp.AddInParameter(cmd, "userID", DbType.String, userID);
            return dbTemp.ExecuteDataSet(cmd).Tables[0];
        }

        /// <summary>
        /// 根据系统标识父菜单ID生成菜单
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="strParentId">父菜单ID</param>
        /// <returns></returns>
        /// <!--
        /// 功能描述  : 菜单的生成与权限挂钩
        /// 修改人  : cd
        /// 修改时间: 2007-07-10
        /// -->
        public  DataTable GenerateChildMenu(string userID, string strParentId)
        {
            string strSql = @"select a.* from sys_menu a join sys_usertype_menu_relation b on a.ID=b.Menu_ID join sys_usertype c on c.id=b.usertype_id join sys_user d on d.Type=c.UserTypeCode  where a.parent_id =:parentid and d.userID=:userID order by order_field";
            DbCommand cmd = dbTemp.GetSqlStringCommand(strSql);
            dbTemp.AddInParameter(cmd, "parentid", DbType.String, strParentId);
            dbTemp.AddInParameter(cmd, "userID", DbType.String, userID);
            return dbTemp.ExecuteDataSet(cmd).Tables[0];
        }


        /// 创建人  : Wu Hansi
        /// 创建时间: 2007-07-09
        /// 修改人  : Wu Hansi
        /// 修改时间: 2007-07-09
        /// 修改描述: 描述                 
        /// <summary>
        /// 得到排序好的所有菜单
        /// </summary>
        /// <returns></returns>
        public  DataTable GetMenusSorted()
        {
            string strSql = @"select t2.parent_id sortid,null parentMenu, t2.menu_title childMenu,t2.id, t2.menu_url, null child
                            from sys_menu t1, sys_menu t2 
                            where t1.ID = t2.parent_id UNION 
                            select t3.id sortid,menu_title parentMenu,null childMenu,id, menu_url,
                            (select count(*) from sys_menu t4 where t4.parent_id=t3.id) child
                            from sys_menu t3 
                            where t3.parent_id is null order by sortid desc,menu_url desc
                            ";
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 根据条件得到排序好的所有菜单
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public  DataTable GetMenusSorted(string str)
        {
            string strSql = @"select t2.parent_id sortid,null parentMenu, t2.menu_title childMenu,t2.id, t2.menu_url, null child
                            from sys_menu t1, sys_menu t2 
                            where t1.ID = t2.parent_id  and t2.menu_title like '%" +str + @"%' UNION 
                            select t3.id sortid,menu_title parentMenu,null childMenu,id, menu_url,
                            (select count(*) from sys_menu t4 where t4.parent_id=t3.id) child
                            from sys_menu t3 
                            where t3.parent_id is null and t3.menu_title like '%" + str + @"%' order by sortid desc,menu_url desc
                            ";
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }
        /// <summary>
        /// 得到排序好的，带是否拥有权限的标志
        /// </summary>
        /// <param name="usertype_id"></param>
        /// <returns></returns>
        public  DataTable GetMenusSortedWithAuth(string usertype_id)
        {
            string strSql = @"select t2.parent_id sortid,null parentmenu, t2.menu_title childmenu,t2.id,t2.menu_url,
                            (select count(*) from sys_usertype_menu_relation s1 where 
                            s1.usertype_id='" + usertype_id + @"' and s1.menu_id=t2.id) auth
                            from sys_menu t1, sys_menu t2 where t1.ID = t2.parent_id
                            UNION 
                            select t3.id sortid,menu_title parentmenu,null childmenu,id,menu_url,
                            (select count(*) from sys_usertype_menu_relation s1 where 
                            s1.usertype_id='" + usertype_id + @"' and s1.menu_id=t3.id) auth
                            from sys_menu t3, sys_usertype_menu_relation s1
                            where t3.parent_id is null order by sortid desc, menu_url desc
                            ";
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 删除现有的菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public  bool DeleteMenu(string id)
        {
            string strSql = @"Delete from sys_menu where id = :row_id";

            DbCommand cmdTemp = dbTemp.GetSqlStringCommand(strSql);
            dbTemp.AddInParameter(cmdTemp, "row_id", DbType.String, id);
            return dbTemp.ExecuteNonQuery(cmdTemp) > 0 ? true : false;
        }

        /// <summary>
        /// 增加一个用户类型-菜单关系
        /// </summary>
        /// <param name="usertype_id"></param>
        /// <param name="menu_id"></param>
        /// <returns></returns>
        public  bool InsertUsertypeMenuRelation(string usertype_id, string menu_id)
        {
            string strSql = @"Insert into sys_usertype_menu_relation (usertype_id, menu_id) VALUES ('" + 
                            usertype_id + "','" + menu_id + "')";
            return dbTemp.ExecuteNonQuery(CommandType.Text, strSql) > 0 ? true : false;
        }

        /// <summary>
        /// 删除一个用户类型-菜单关系
        /// </summary>
        /// <param name="usertype_id"></param>
        /// <param name="menu_id"></param>
        /// <returns></returns>
        public bool RemoveUsertypeMenuRelation(string usertype_id, string menu_id)
        {
            string strSql = string.Format(@"Delete from sys_usertype_menu_relation where 
                usertype_id ='{0}' and menu_id='{1}' ",usertype_id,menu_id);

            return dbTemp.ExecuteNonQuery(CommandType.Text, strSql) > 0 ? true : false;
        }

        /// <summary>
        /// 由主键来删除一条用户类型-菜单关系记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        
        // 创建人  ：zw
        // 创建时间：2007-07-26
        public  bool DeleteMenuUserType(string id)
        {
            string strSql = @"delete from sys_usertype where id=:row_id";

            DbCommand cmdTemp = dbTemp.GetSqlStringCommand(strSql);
            dbTemp.AddInParameter(cmdTemp, "row_id", DbType.String, id);
            return dbTemp.ExecuteNonQuery(cmdTemp) > 0 ? true : false;
        }

        /// <summary>
        /// 根据菜单id获取父菜单id
        /// </summary>
        /// <param name="strMenuId">菜单id</param>
        /// <returns></returns>
        public  string GetParentId(string strMenuId)
        {
            string strSql =string.Format(@"select PARENT_ID from SYS_MENU where id='{0}'",strMenuId);
            return dbTemp.ExecuteScalar(CommandType.Text, strSql).ToString();
        }

        /// <summary>
        ///获取特定菜单下的子菜单
        /// </summary>
        /// <param name="strParentMenuName">Name of the STR parent menu.</param>
        /// <returns></returns>
        public DataTable GetChildMenus(string strParentMenuName)
        {
            string strSql = string.Format(@"select * from SYS_MENU where parent_id in 
                (select id from sys_menu where menu_title='{0}') and isenable=1 order by order_field", strParentMenuName);
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 根据流程和菜单获取下面有权限的菜单
        /// </summary>
        /// <param name="strParentMenuName"></param>
        /// <returns></returns>
        public DataTable GetChildMenus(string strParentMenuName,string strFlowName)
        {
            string strSql = string.Format(@"select *
  from SYS_MENU
 where parent_id in (select id from sys_menu where menu_title = '{0}')
   and isenable = 1
   and menu_title in (select menu_name
                        from XT_WORKFLOW_SET a, xt_workflow_define b
                       where a.flowid = b.id
                         and b.ispub = '1'
                         and a.menu_appear = '1'
                         and b.flowname = '{1}')
 order by order_field", strParentMenuName, strFlowName);
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 获取个人信息管理菜单(非单位管理员)
        /// </summary>
        /// <returns></returns>
        /// <!--addby zhongjian 20100125-->
        public DataTable GetChildMenusPersonal()
        {
            string strSql = string.Format(@"select * from SYS_MENU where parent_id in 
                (select id from sys_menu where menu_title='个人信息管理') and ISENABLE='1' order by order_field");
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 根据流程分类获得行政许可事项
        /// </summary>
        /// <param name="strFlowName">流程分类</param>
        /// <param name="strMenuTitle">标题</param>
        /// <returns></returns>
        public string GetFlow(string strMenuTitle, string strFlowName)
        {
            string strSql = string.Format(@"select {0} from xt_workflow_define where flowname='{1}'", strMenuTitle, strFlowName);
            return dbTemp.ExecuteScalar(CommandType.Text, strSql).ToString();
        }

        /// <summary>
        /// 根据用户ID判断该用户是否单位管理员用户
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        /// <!--addby zhongjian 20100125-->
        public DataTable SelectUnitsAdmin(string UserID)
        {
            string strSql = string.Format(@"select * from sys_user where usertype='1' and unitsid is not null
                                            and unitsadmin='1' and userid='{0}'", UserID);
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 根据流程别名获取该流程拓展信息
        /// </summary>
        /// <param name="strFlowType">流程别名</param>
        /// <returns></returns>
        /// <!--addby zhongjian 20100308-->
        public DataTable GetWorkflowDefine(string strFlowType)
        {
            string strSql = string.Format(@"select flowname, flowtype, id, interfaceurl, interfacetype
                                              from xt_workflow_define
                                             where ispub = 1
                                               and isdelete = 0
                                               and flowtype = '{0}'", strFlowType);
            return dbTemp.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }
    }
}
